﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DWEIP.Common;

namespace DWEIP.BLL.Sys
{
    public partial class SysConfigBLL
    {
        private static object lockHelper = new object();

        /// <summary>
        /// 這裏先不寫Dal模塊
        /// </summary>
        /// <param name="configFilePath"></param>
        /// <returns></returns>
        public DWEIP.Models.Sys.SysConfigModel dalLoadConfig(string configFilePath)
        {
            return (DWEIP.Models.Sys.SysConfigModel)SerializationHelper.Load(typeof(DWEIP.Models.Sys.SysConfigModel), configFilePath);
        }
        /// <summary>
        ///  读取配置文件
        /// </summary>
        public DWEIP.Models.Sys.SysConfigModel loadConfig(string configFilePath)
        {
            DWEIP.Models.Sys.SysConfigModel model = CacheHelper.Get<DWEIP.Models.Sys.SysConfigModel>(ContextKeys.CACHE_SITE_CONFIG);
            if (model == null)
            {
                CacheHelper.Insert(ContextKeys.CACHE_SITE_CONFIG, dalLoadConfig(configFilePath), configFilePath);
                model = CacheHelper.Get<DWEIP.Models.Sys.SysConfigModel>(ContextKeys.CACHE_SITE_CONFIG);
            }
            return model;
        }

        /// <summary>
        /// 读取客户端站点配置信息
        /// </summary>
        public DWEIP.Models.Sys.SysConfigModel loadConfig(string configFilePath, bool isClient)
        {
            DWEIP.Models.Sys.SysConfigModel model = CacheHelper.Get<DWEIP.Models.Sys.SysConfigModel>(ContextKeys.CACHE_SITE_CONFIG_CLIENT);
            if (model == null)
            {
                model = dalLoadConfig(configFilePath);
                model.templateskin = model.webpath + "templates/" + model.templateskin;
                CacheHelper.Insert(ContextKeys.CACHE_SITE_CONFIG_CLIENT, model, configFilePath);
            }
            return model;
        }

        /// <summary>
        ///  保存配置文件
        /// </summary>
        public DWEIP.Models.Sys.SysConfigModel saveConifg(DWEIP.Models.Sys.SysConfigModel model, string configFilePath)
        {
            lock (lockHelper)
            {
                SerializationHelper.Save(model, configFilePath);
            }
            return model;
        }

    }
}
